#include "includes.h"
#include "structs.h"
#include "declares.h"

/* the functions in this file use the grape_inter_logger
 * to print intermedia result to stderr or log file
 * depending on --verbose flag
 */

void printl_fragments( struct Fragment *fragments, int nfrag)
{
        struct vertex_t *pv;
        zlog_debug(grape_inter_logger,"fragments(nfrag=%d\n\n",nfrag);
        zlog_debug(grape_inter_logger, "-----------------------------------------------------------------------------------\n");
        for(int i=0;i<nfrag;++i) {
                zlog_debug(grape_inter_logger,"\tfragment(fid=%d)\n\n\tlocal_adjlist=\n", fragments[i].fid);
                int size=igraph_vector_ptr_size((fragments[i].innerv));
                zlog_debug(grape_inter_logger, "\t\t");
                printl_my_adjlist(fragments[i].local_adjlist);
                zlog_debug(grape_inter_logger, "\tinner_vertices=\n");
                for(int j=0;j<size;++j) {
                        zlog_debug(grape_inter_logger, "\t\t");
                        /* struct vertex_t *pv=VECTOR(*(fragments[i].innerv))[j]; */
                        pv=igraph_vector_ptr_e(fragments[i].innerv, j);
                        printl_vertex(pv);
                }
                zlog_debug(grape_inter_logger, "-----------------------------------------------------------------------------------\n");
        }
        zlog_debug(grape_inter_logger, ")\n");
}

/* print textual info of a my_adjlist_t */
void printl_my_adjlist( my_adjlist_t *al)
{
        int vn = al ->vn;
        zlog_debug(grape_inter_logger, "my_adjlist_t(vn=%d)\n", al->vn);
        for(int i=0;i<vn;++i) {
                igraph_vector_ptr_t *pa = &(al->al[i]);
                int size = igraph_vector_ptr_size(pa);
                zlog_debug(grape_inter_logger, "\tvid=%d\n", i+1);
                for(int j=0;j<size;++j) {
                        struct edge_t *pe = VECTOR(*pa)[j];
                        /* zlog_debug(grape_inter_logger,"%d ", pe->dst+1); */
                        zlog_debug(grape_inter_logger, "\t\t");
                        printl_edge(pe);
                }
                zlog_debug(grape_inter_logger, "\n");
        }
}


void printl_pair(void *ptr)
{
        struct pair_t *p=ptr;
        zlog_debug(grape_inter_logger,"my_pair(vid=%d,dist=%d)\n", 
                        p->vid,
                        p->dist);
}

void printl_vertex(struct vertex_t* v)
{
        zlog_debug(grape_inter_logger,"vertex(vid=%d,ncon=%d,vsize=%d,attr=%p,fid=%d,locid=%d)fids=\n",
                        v->vid + 1,
                        v->ncon,
                        v->vsize,
                        v->attr,
                        v->fid,
                        v->locid);
        /* igraph_vector_int_fprint(v->fids, */ 

}

void printl_edge(struct edge_t *pedge)
{

        zlog_debug(grape_inter_logger, "edge(eid=%d,src=%d,dst=%d,attr=%d)\n",
                        pedge->eid + 1,
                        pedge->src + 1,
                        pedge->dst + 1,
                        pedge->attr);
}


void printl_msg_buf(struct msg_buf_t *msg)
{
        int nfrag=msg->nfrag;
        zlog_debug(grape_inter_logger,"msg_buf_t(nfrag=%d)\ninmsg=(inmsg_size=%ld)\n",msg->nfrag,
                        igraph_vector_ptr_size(msg->inmsg));
        int size=igraph_vector_ptr_size(msg->inmsg);
        for(int i=0;i<size;++i) {
                printl_pair(igraph_vector_ptr_e(msg->inmsg,i));
        }
        zlog_debug(grape_inter_logger,"\noutmsg=(outmsg_size=%ld)\n",
                        igraph_vector_ptr_size(msg->outmsg));
        
        for(int i=0;i<nfrag;++i) {
                zlog_debug(grape_inter_logger,"\nTO fid=%d\n", i);
                void *ptr=igraph_vector_ptr_e(msg->outmsg,i);
                int size=igraph_vector_ptr_size(ptr);
                for(int j=0;j<size;++j) {
                        grape_plugin->msg_log_func(igraph_vector_ptr_e(ptr,j));
                        /* grape_msg_log(igraph_vector_ptr_e(ptr,j)); */
                }
        }
        zlog_debug(grape_inter_logger, "\n");
}
        
void printl_msg_buf_all(struct msg_buf_t *msg)
{
        int nfrag=msg[0].nfrag;

        zlog_debug(grape_inter_logger, "BUFFERS\n---------------------------------------------------------\n");
        for(int i=0;i<nfrag;++i) {
                zlog_debug(grape_inter_logger,"MESSAGE BUFFER %d\n", i);
                printl_msg_buf(&msg[i]);
                zlog_debug(grape_inter_logger, "---------------------------------------------------------\n");
        }
}

